<html>
  <head>
  <title>textGridworldDisplay.py</title>
  </head>
  <body>
  <h3>textGridworldDisplay.py</h3>
  <hr>
  <pre>
<span style="color: blue; font-weight: bold">import </span>util

<span style="color: blue; font-weight: bold">class </span>TextGridworldDisplay<span style="font-weight: bold">:
  
  </span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>gridworld<span style="font-weight: bold">):
    </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>gridworld <span style="font-weight: bold">= </span>gridworld
  
  <span style="color: blue; font-weight: bold">def </span>start<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">pass
  
  def </span>pause<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">pass
  
  def </span>displayValues<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>agent<span style="font-weight: bold">, </span>currentState <span style="font-weight: bold">= </span><span style="color: blue">None</span><span style="font-weight: bold">, </span>message <span style="font-weight: bold">= </span><span style="color: blue">None</span><span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">if </span>message <span style="font-weight: bold">!= </span><span style="color: blue">None</span><span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">print </span>message
    values <span style="font-weight: bold">= </span>util<span style="font-weight: bold">.</span>Counter<span style="font-weight: bold">()
    </span>policy <span style="font-weight: bold">= {}
    </span>states <span style="font-weight: bold">= </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>gridworld<span style="font-weight: bold">.</span>getStates<span style="font-weight: bold">()
    </span><span style="color: blue; font-weight: bold">for </span>state <span style="color: blue; font-weight: bold">in </span>states<span style="font-weight: bold">:
      </span>values<span style="font-weight: bold">[</span>state<span style="font-weight: bold">] = </span>agent<span style="font-weight: bold">.</span>getValue<span style="font-weight: bold">(</span>state<span style="font-weight: bold">)
      </span>policy<span style="font-weight: bold">[</span>state<span style="font-weight: bold">] = </span>agent<span style="font-weight: bold">.</span>getPolicy<span style="font-weight: bold">(</span>state<span style="font-weight: bold">)
    </span>prettyPrintValues<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>gridworld<span style="font-weight: bold">, </span>values<span style="font-weight: bold">, </span>policy<span style="font-weight: bold">, </span>currentState<span style="font-weight: bold">)
  
  </span><span style="color: blue; font-weight: bold">def </span>displayNullValues<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>agent<span style="font-weight: bold">, </span>currentState <span style="font-weight: bold">= </span><span style="color: blue">None</span><span style="font-weight: bold">, </span>message <span style="font-weight: bold">= </span><span style="color: blue">None</span><span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">if </span>message <span style="font-weight: bold">!= </span><span style="color: blue">None</span><span style="font-weight: bold">: </span><span style="color: blue; font-weight: bold">print </span>message
    prettyPrintNullValues<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>gridworld<span style="font-weight: bold">, </span>currentState<span style="font-weight: bold">)

  </span><span style="color: blue; font-weight: bold">def </span>displayQValues<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>agent<span style="font-weight: bold">, </span>currentState <span style="font-weight: bold">= </span><span style="color: blue">None</span><span style="font-weight: bold">, </span>message <span style="font-weight: bold">= </span><span style="color: blue">None</span><span style="font-weight: bold">):
    </span><span style="color: blue; font-weight: bold">if </span>message <span style="font-weight: bold">!= </span><span style="color: blue">None</span><span style="font-weight: bold">: </span><span style="color: blue; font-weight: bold">print </span>message
    qValues <span style="font-weight: bold">= </span>util<span style="font-weight: bold">.</span>Counter<span style="font-weight: bold">()
    </span>states <span style="font-weight: bold">= </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>gridworld<span style="font-weight: bold">.</span>getStates<span style="font-weight: bold">()
    </span><span style="color: blue; font-weight: bold">for </span>state <span style="color: blue; font-weight: bold">in </span>states<span style="font-weight: bold">:
      </span><span style="color: blue; font-weight: bold">for </span>action <span style="color: blue; font-weight: bold">in </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>gridworld<span style="font-weight: bold">.</span>getPossibleActions<span style="font-weight: bold">(</span>state<span style="font-weight: bold">):
        </span>qValues<span style="font-weight: bold">[(</span>state<span style="font-weight: bold">, </span>action<span style="font-weight: bold">)] = </span>agent<span style="font-weight: bold">.</span>getQValue<span style="font-weight: bold">(</span>state<span style="font-weight: bold">, </span>action<span style="font-weight: bold">)
    </span>prettyPrintQValues<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>gridworld<span style="font-weight: bold">, </span>qValues<span style="font-weight: bold">, </span>currentState<span style="font-weight: bold">)


</span><span style="color: blue; font-weight: bold">def </span>prettyPrintValues<span style="font-weight: bold">(</span>gridWorld<span style="font-weight: bold">, </span>values<span style="font-weight: bold">, </span>policy<span style="font-weight: bold">=</span><span style="color: blue">None</span><span style="font-weight: bold">, </span>currentState <span style="font-weight: bold">= </span><span style="color: blue">None</span><span style="font-weight: bold">):
  </span>grid <span style="font-weight: bold">= </span>gridWorld<span style="font-weight: bold">.</span>grid
  maxLen <span style="font-weight: bold">= </span><span style="color: red">11
  </span>newRows <span style="font-weight: bold">= []
  </span><span style="color: blue; font-weight: bold">for </span>y <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>grid<span style="font-weight: bold">.</span>height<span style="font-weight: bold">):
    </span>newRow <span style="font-weight: bold">= []
    </span><span style="color: blue; font-weight: bold">for </span>x <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>grid<span style="font-weight: bold">.</span>width<span style="font-weight: bold">):
      </span>state <span style="font-weight: bold">= (</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">)
      </span>value <span style="font-weight: bold">= </span>values<span style="font-weight: bold">[</span>state<span style="font-weight: bold">]
      </span>action <span style="font-weight: bold">= </span><span style="color: blue">None
      </span><span style="color: blue; font-weight: bold">if </span>policy <span style="font-weight: bold">!= </span><span style="color: blue">None </span><span style="color: blue; font-weight: bold">and </span>state <span style="color: blue; font-weight: bold">in </span>policy<span style="font-weight: bold">:
        </span>action <span style="font-weight: bold">= </span>policy<span style="font-weight: bold">[</span>state<span style="font-weight: bold">]          
      </span>actions <span style="font-weight: bold">= </span>gridWorld<span style="font-weight: bold">.</span>getPossibleActions<span style="font-weight: bold">(</span>state<span style="font-weight: bold">)        
      </span><span style="color: blue; font-weight: bold">if </span>action <span style="color: blue; font-weight: bold">not in </span>actions <span style="color: blue; font-weight: bold">and </span><span style="color: red">'exit' </span><span style="color: blue; font-weight: bold">in </span>actions<span style="font-weight: bold">:
        </span>action <span style="font-weight: bold">= </span><span style="color: red">'exit'
      </span>valString <span style="font-weight: bold">= </span><span style="color: blue">None
      </span><span style="color: blue; font-weight: bold">if </span>action <span style="font-weight: bold">== </span><span style="color: red">'exit'</span><span style="font-weight: bold">:
        </span>valString <span style="font-weight: bold">= </span>border<span style="font-weight: bold">(</span><span style="color: red">'%.2f' </span><span style="font-weight: bold">% </span>value<span style="font-weight: bold">)
      </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
        </span>valString <span style="font-weight: bold">= </span><span style="color: red">'\n\n%.2f\n\n' </span><span style="font-weight: bold">% </span>value
        valString <span style="font-weight: bold">+= </span><span style="color: red">' '</span><span style="font-weight: bold">*</span>maxLen
      <span style="color: blue; font-weight: bold">if </span>grid<span style="font-weight: bold">[</span>x<span style="font-weight: bold">][</span>y<span style="font-weight: bold">] == </span><span style="color: red">'S'</span><span style="font-weight: bold">:
        </span>valString <span style="font-weight: bold">= </span><span style="color: red">'\n\nS: %.2f\n\n'  </span><span style="font-weight: bold">% </span>value
        valString <span style="font-weight: bold">+= </span><span style="color: red">' '</span><span style="font-weight: bold">*</span>maxLen        
      <span style="color: blue; font-weight: bold">if </span>grid<span style="font-weight: bold">[</span>x<span style="font-weight: bold">][</span>y<span style="font-weight: bold">] == </span><span style="color: red">'#'</span><span style="font-weight: bold">:
        </span>valString <span style="font-weight: bold">= </span><span style="color: red">'\n#####\n#####\n#####\n'
        </span>valString <span style="font-weight: bold">+= </span><span style="color: red">' '</span><span style="font-weight: bold">*</span>maxLen
      pieces <span style="font-weight: bold">= [</span>valString<span style="font-weight: bold">]                
      </span>text <span style="font-weight: bold">= (</span><span style="color: red">"\n"</span><span style="font-weight: bold">.</span>join<span style="font-weight: bold">(</span>pieces<span style="font-weight: bold">)).</span>split<span style="font-weight: bold">(</span><span style="color: red">'\n'</span><span style="font-weight: bold">)        
      </span><span style="color: blue; font-weight: bold">if </span>currentState <span style="font-weight: bold">== </span>state<span style="font-weight: bold">:
        </span>l <span style="font-weight: bold">= </span>len<span style="font-weight: bold">(</span>text<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">])
        </span><span style="color: blue; font-weight: bold">if </span>l <span style="font-weight: bold">== </span><span style="color: red">0</span><span style="font-weight: bold">:
          </span>text<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">] = </span><span style="color: red">'*'
        </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
          </span>text<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">] = </span><span style="color: red">"|" </span><span style="font-weight: bold">+ </span><span style="color: red">' ' </span><span style="font-weight: bold">* </span>int<span style="font-weight: bold">((</span>l<span style="font-weight: bold">-</span><span style="color: red">1</span><span style="font-weight: bold">)/</span><span style="color: red">2</span><span style="font-weight: bold">-</span><span style="color: red">1</span><span style="font-weight: bold">) + </span><span style="color: red">'*' </span><span style="font-weight: bold">+ </span><span style="color: red">' ' </span><span style="font-weight: bold">* </span>int<span style="font-weight: bold">((</span>l<span style="font-weight: bold">)/</span><span style="color: red">2</span><span style="font-weight: bold">-</span><span style="color: red">1</span><span style="font-weight: bold">) + </span><span style="color: red">"|"       
      </span><span style="color: blue; font-weight: bold">if </span>action <span style="font-weight: bold">== </span><span style="color: red">'east'</span><span style="font-weight: bold">:
        </span>text<span style="font-weight: bold">[</span><span style="color: red">2</span><span style="font-weight: bold">] = </span><span style="color: red">'  ' </span><span style="font-weight: bold">+ </span>text<span style="font-weight: bold">[</span><span style="color: red">2</span><span style="font-weight: bold">]  + </span><span style="color: red">' &gt;'
      </span><span style="color: blue; font-weight: bold">elif </span>action <span style="font-weight: bold">== </span><span style="color: red">'west'</span><span style="font-weight: bold">:
        </span>text<span style="font-weight: bold">[</span><span style="color: red">2</span><span style="font-weight: bold">] = </span><span style="color: red">'&lt; ' </span><span style="font-weight: bold">+ </span>text<span style="font-weight: bold">[</span><span style="color: red">2</span><span style="font-weight: bold">]  + </span><span style="color: red">'  '
      </span><span style="color: blue; font-weight: bold">elif </span>action <span style="font-weight: bold">== </span><span style="color: red">'north'</span><span style="font-weight: bold">:
        </span>text<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">] = </span><span style="color: red">' ' </span><span style="font-weight: bold">* </span>int<span style="font-weight: bold">(</span>maxLen<span style="font-weight: bold">/</span><span style="color: red">2</span><span style="font-weight: bold">) + </span><span style="color: red">'^' </span><span style="font-weight: bold">+</span><span style="color: red">' ' </span><span style="font-weight: bold">* </span>int<span style="font-weight: bold">(</span>maxLen<span style="font-weight: bold">/</span><span style="color: red">2</span><span style="font-weight: bold">)
      </span><span style="color: blue; font-weight: bold">elif </span>action <span style="font-weight: bold">== </span><span style="color: red">'south'</span><span style="font-weight: bold">:
        </span>text<span style="font-weight: bold">[</span><span style="color: red">4</span><span style="font-weight: bold">] = </span><span style="color: red">' ' </span><span style="font-weight: bold">* </span>int<span style="font-weight: bold">(</span>maxLen<span style="font-weight: bold">/</span><span style="color: red">2</span><span style="font-weight: bold">) + </span><span style="color: red">'v' </span><span style="font-weight: bold">+</span><span style="color: red">' ' </span><span style="font-weight: bold">* </span>int<span style="font-weight: bold">(</span>maxLen<span style="font-weight: bold">/</span><span style="color: red">2</span><span style="font-weight: bold">)
      </span>newCell <span style="font-weight: bold">= </span><span style="color: red">"\n"</span><span style="font-weight: bold">.</span>join<span style="font-weight: bold">(</span>text<span style="font-weight: bold">)
      </span>newRow<span style="font-weight: bold">.</span>append<span style="font-weight: bold">(</span>newCell<span style="font-weight: bold">)
    </span>newRows<span style="font-weight: bold">.</span>append<span style="font-weight: bold">(</span>newRow<span style="font-weight: bold">)
  </span>numCols <span style="font-weight: bold">= </span>grid<span style="font-weight: bold">.</span>width
  <span style="color: blue; font-weight: bold">for </span>rowNum<span style="font-weight: bold">, </span>row <span style="color: blue; font-weight: bold">in </span>enumerate<span style="font-weight: bold">(</span>newRows<span style="font-weight: bold">):
    </span>row<span style="font-weight: bold">.</span>insert<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">"\n\n"</span><span style="font-weight: bold">+</span>str<span style="font-weight: bold">(</span>rowNum<span style="font-weight: bold">))
  </span>newRows<span style="font-weight: bold">.</span>reverse<span style="font-weight: bold">()
  </span>colLabels <span style="font-weight: bold">= [</span>str<span style="font-weight: bold">(</span>colNum<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>colNum <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>numCols<span style="font-weight: bold">)]
  </span>colLabels<span style="font-weight: bold">.</span>insert<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">' '</span><span style="font-weight: bold">)
  </span>finalRows <span style="font-weight: bold">= [</span>colLabels<span style="font-weight: bold">] + </span>newRows
  <span style="color: blue; font-weight: bold">print </span>indent<span style="font-weight: bold">(</span>finalRows<span style="font-weight: bold">,</span>separateRows<span style="font-weight: bold">=</span><span style="color: blue; font-weight: bold">True</span><span style="font-weight: bold">,</span>delim<span style="font-weight: bold">=</span><span style="color: red">'|'</span><span style="font-weight: bold">, </span>prefix<span style="font-weight: bold">=</span><span style="color: red">'|'</span><span style="font-weight: bold">,</span>postfix<span style="font-weight: bold">=</span><span style="color: red">'|'</span><span style="font-weight: bold">, </span>justify<span style="font-weight: bold">=</span><span style="color: red">'center'</span><span style="font-weight: bold">,</span>hasHeader<span style="font-weight: bold">=</span><span style="color: blue; font-weight: bold">True</span><span style="font-weight: bold">)


</span><span style="color: blue; font-weight: bold">def </span>prettyPrintNullValues<span style="font-weight: bold">(</span>gridWorld<span style="font-weight: bold">, </span>currentState <span style="font-weight: bold">= </span><span style="color: blue">None</span><span style="font-weight: bold">):
    </span>grid <span style="font-weight: bold">= </span>gridWorld<span style="font-weight: bold">.</span>grid
    maxLen <span style="font-weight: bold">= </span><span style="color: red">11
    </span>newRows <span style="font-weight: bold">= []
    </span><span style="color: blue; font-weight: bold">for </span>y <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>grid<span style="font-weight: bold">.</span>height<span style="font-weight: bold">):
      </span>newRow <span style="font-weight: bold">= []
      </span><span style="color: blue; font-weight: bold">for </span>x <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>grid<span style="font-weight: bold">.</span>width<span style="font-weight: bold">):
        </span>state <span style="font-weight: bold">= (</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">)

        </span><span style="color: green; font-style: italic"># value = values[state]

        </span>action <span style="font-weight: bold">= </span><span style="color: blue">None
        </span><span style="color: green; font-style: italic"># if policy != None and state in policy:
        #   action = policy[state]
        # 
        </span>actions <span style="font-weight: bold">= </span>gridWorld<span style="font-weight: bold">.</span>getPossibleActions<span style="font-weight: bold">(</span>state<span style="font-weight: bold">)

        </span><span style="color: blue; font-weight: bold">if </span>action <span style="color: blue; font-weight: bold">not in </span>actions <span style="color: blue; font-weight: bold">and </span><span style="color: red">'exit' </span><span style="color: blue; font-weight: bold">in </span>actions<span style="font-weight: bold">:
          </span>action <span style="font-weight: bold">= </span><span style="color: red">'exit'

        </span>valString <span style="font-weight: bold">= </span><span style="color: blue">None
        </span><span style="color: green; font-style: italic"># if action == 'exit':
        #   valString = border('%.2f' % value)
        # else:
        #   valString = '\n\n%.2f\n\n' % value
        #   valString += ' '*maxLen

        </span><span style="color: blue; font-weight: bold">if </span>grid<span style="font-weight: bold">[</span>x<span style="font-weight: bold">][</span>y<span style="font-weight: bold">] == </span><span style="color: red">'S'</span><span style="font-weight: bold">:
          </span>valString <span style="font-weight: bold">= </span><span style="color: red">'\n\nS\n\n'
          </span>valString <span style="font-weight: bold">+= </span><span style="color: red">' '</span><span style="font-weight: bold">*</span>maxLen
        <span style="color: blue; font-weight: bold">elif </span>grid<span style="font-weight: bold">[</span>x<span style="font-weight: bold">][</span>y<span style="font-weight: bold">] == </span><span style="color: red">'#'</span><span style="font-weight: bold">:
          </span>valString <span style="font-weight: bold">= </span><span style="color: red">'\n#####\n#####\n#####\n'
          </span>valString <span style="font-weight: bold">+= </span><span style="color: red">' '</span><span style="font-weight: bold">*</span>maxLen
        <span style="color: blue; font-weight: bold">elif </span>type<span style="font-weight: bold">(</span>grid<span style="font-weight: bold">[</span>x<span style="font-weight: bold">][</span>y<span style="font-weight: bold">]) == </span>float <span style="color: blue; font-weight: bold">or </span>type<span style="font-weight: bold">(</span>grid<span style="font-weight: bold">[</span>x<span style="font-weight: bold">][</span>y<span style="font-weight: bold">]) == </span>int<span style="font-weight: bold">:
          </span>valString <span style="font-weight: bold">= </span>border<span style="font-weight: bold">(</span><span style="color: red">'%.2f' </span><span style="font-weight: bold">% </span>float<span style="font-weight: bold">(</span>grid<span style="font-weight: bold">[</span>x<span style="font-weight: bold">][</span>y<span style="font-weight: bold">]))
        </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">: </span>valString <span style="font-weight: bold">= </span>border<span style="font-weight: bold">(</span><span style="color: red">'  '</span><span style="font-weight: bold">)
        </span>pieces <span style="font-weight: bold">= [</span>valString<span style="font-weight: bold">]

        </span>text <span style="font-weight: bold">= (</span><span style="color: red">"\n"</span><span style="font-weight: bold">.</span>join<span style="font-weight: bold">(</span>pieces<span style="font-weight: bold">)).</span>split<span style="font-weight: bold">(</span><span style="color: red">'\n'</span><span style="font-weight: bold">)

        </span><span style="color: blue; font-weight: bold">if </span>currentState <span style="font-weight: bold">== </span>state<span style="font-weight: bold">:
          </span>l <span style="font-weight: bold">= </span>len<span style="font-weight: bold">(</span>text<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">])
          </span><span style="color: blue; font-weight: bold">if </span>l <span style="font-weight: bold">== </span><span style="color: red">0</span><span style="font-weight: bold">:
            </span>text<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">] = </span><span style="color: red">'*'
          </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
            </span>text<span style="font-weight: bold">[</span><span style="color: red">1</span><span style="font-weight: bold">] = </span><span style="color: red">"|" </span><span style="font-weight: bold">+ </span><span style="color: red">' ' </span><span style="font-weight: bold">* </span>int<span style="font-weight: bold">((</span>l<span style="font-weight: bold">-</span><span style="color: red">1</span><span style="font-weight: bold">)/</span><span style="color: red">2</span><span style="font-weight: bold">-</span><span style="color: red">1</span><span style="font-weight: bold">) + </span><span style="color: red">'*' </span><span style="font-weight: bold">+ </span><span style="color: red">' ' </span><span style="font-weight: bold">* </span>int<span style="font-weight: bold">((</span>l<span style="font-weight: bold">)/</span><span style="color: red">2</span><span style="font-weight: bold">-</span><span style="color: red">1</span><span style="font-weight: bold">) + </span><span style="color: red">"|"

        </span><span style="color: blue; font-weight: bold">if </span>action <span style="font-weight: bold">== </span><span style="color: red">'east'</span><span style="font-weight: bold">:
          </span>text<span style="font-weight: bold">[</span><span style="color: red">2</span><span style="font-weight: bold">] = </span><span style="color: red">'  ' </span><span style="font-weight: bold">+ </span>text<span style="font-weight: bold">[</span><span style="color: red">2</span><span style="font-weight: bold">]  + </span><span style="color: red">' &gt;'
        </span><span style="color: blue; font-weight: bold">elif </span>action <span style="font-weight: bold">== </span><span style="color: red">'west'</span><span style="font-weight: bold">:
          </span>text<span style="font-weight: bold">[</span><span style="color: red">2</span><span style="font-weight: bold">] = </span><span style="color: red">'&lt; ' </span><span style="font-weight: bold">+ </span>text<span style="font-weight: bold">[</span><span style="color: red">2</span><span style="font-weight: bold">]  + </span><span style="color: red">'  '
        </span><span style="color: blue; font-weight: bold">elif </span>action <span style="font-weight: bold">== </span><span style="color: red">'north'</span><span style="font-weight: bold">:
          </span>text<span style="font-weight: bold">[</span><span style="color: red">0</span><span style="font-weight: bold">] = </span><span style="color: red">' ' </span><span style="font-weight: bold">* </span>int<span style="font-weight: bold">(</span>maxLen<span style="font-weight: bold">/</span><span style="color: red">2</span><span style="font-weight: bold">) + </span><span style="color: red">'^' </span><span style="font-weight: bold">+</span><span style="color: red">' ' </span><span style="font-weight: bold">* </span>int<span style="font-weight: bold">(</span>maxLen<span style="font-weight: bold">/</span><span style="color: red">2</span><span style="font-weight: bold">)
        </span><span style="color: blue; font-weight: bold">elif </span>action <span style="font-weight: bold">== </span><span style="color: red">'south'</span><span style="font-weight: bold">:
          </span>text<span style="font-weight: bold">[</span><span style="color: red">4</span><span style="font-weight: bold">] = </span><span style="color: red">' ' </span><span style="font-weight: bold">* </span>int<span style="font-weight: bold">(</span>maxLen<span style="font-weight: bold">/</span><span style="color: red">2</span><span style="font-weight: bold">) + </span><span style="color: red">'v' </span><span style="font-weight: bold">+</span><span style="color: red">' ' </span><span style="font-weight: bold">* </span>int<span style="font-weight: bold">(</span>maxLen<span style="font-weight: bold">/</span><span style="color: red">2</span><span style="font-weight: bold">)
        </span>newCell <span style="font-weight: bold">= </span><span style="color: red">"\n"</span><span style="font-weight: bold">.</span>join<span style="font-weight: bold">(</span>text<span style="font-weight: bold">)
        </span>newRow<span style="font-weight: bold">.</span>append<span style="font-weight: bold">(</span>newCell<span style="font-weight: bold">)
      </span>newRows<span style="font-weight: bold">.</span>append<span style="font-weight: bold">(</span>newRow<span style="font-weight: bold">)
    </span>numCols <span style="font-weight: bold">= </span>grid<span style="font-weight: bold">.</span>width
    <span style="color: blue; font-weight: bold">for </span>rowNum<span style="font-weight: bold">, </span>row <span style="color: blue; font-weight: bold">in </span>enumerate<span style="font-weight: bold">(</span>newRows<span style="font-weight: bold">):
      </span>row<span style="font-weight: bold">.</span>insert<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">"\n\n"</span><span style="font-weight: bold">+</span>str<span style="font-weight: bold">(</span>rowNum<span style="font-weight: bold">))
    </span>newRows<span style="font-weight: bold">.</span>reverse<span style="font-weight: bold">()
    </span>colLabels <span style="font-weight: bold">= [</span>str<span style="font-weight: bold">(</span>colNum<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>colNum <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>numCols<span style="font-weight: bold">)]
    </span>colLabels<span style="font-weight: bold">.</span>insert<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">' '</span><span style="font-weight: bold">)
    </span>finalRows <span style="font-weight: bold">= [</span>colLabels<span style="font-weight: bold">] + </span>newRows
    <span style="color: blue; font-weight: bold">print </span>indent<span style="font-weight: bold">(</span>finalRows<span style="font-weight: bold">,</span>separateRows<span style="font-weight: bold">=</span><span style="color: blue; font-weight: bold">True</span><span style="font-weight: bold">,</span>delim<span style="font-weight: bold">=</span><span style="color: red">'|'</span><span style="font-weight: bold">, </span>prefix<span style="font-weight: bold">=</span><span style="color: red">'|'</span><span style="font-weight: bold">,</span>postfix<span style="font-weight: bold">=</span><span style="color: red">'|'</span><span style="font-weight: bold">, </span>justify<span style="font-weight: bold">=</span><span style="color: red">'center'</span><span style="font-weight: bold">,</span>hasHeader<span style="font-weight: bold">=</span><span style="color: blue; font-weight: bold">True</span><span style="font-weight: bold">)
  
</span><span style="color: blue; font-weight: bold">def </span>prettyPrintQValues<span style="font-weight: bold">(</span>gridWorld<span style="font-weight: bold">, </span>qValues<span style="font-weight: bold">, </span>currentState<span style="font-weight: bold">=</span><span style="color: blue">None</span><span style="font-weight: bold">):
    </span>grid <span style="font-weight: bold">= </span>gridWorld<span style="font-weight: bold">.</span>grid
    maxLen <span style="font-weight: bold">= </span><span style="color: red">11
    </span>newRows <span style="font-weight: bold">= []
    </span><span style="color: blue; font-weight: bold">for </span>y <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>grid<span style="font-weight: bold">.</span>height<span style="font-weight: bold">):
      </span>newRow <span style="font-weight: bold">= []
      </span><span style="color: blue; font-weight: bold">for </span>x <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>grid<span style="font-weight: bold">.</span>width<span style="font-weight: bold">):
        </span>state <span style="font-weight: bold">= (</span>x<span style="font-weight: bold">, </span>y<span style="font-weight: bold">)
        </span>actions <span style="font-weight: bold">= </span>gridWorld<span style="font-weight: bold">.</span>getPossibleActions<span style="font-weight: bold">(</span>state<span style="font-weight: bold">)
        </span><span style="color: blue; font-weight: bold">if </span>actions <span style="font-weight: bold">== </span><span style="color: blue">None </span><span style="color: blue; font-weight: bold">or </span>len<span style="font-weight: bold">(</span>actions<span style="font-weight: bold">) == </span><span style="color: red">0</span><span style="font-weight: bold">:
          </span>actions <span style="font-weight: bold">= [</span><span style="color: blue">None</span><span style="font-weight: bold">]
        </span>bestQ <span style="font-weight: bold">= </span>max<span style="font-weight: bold">([</span>qValues<span style="font-weight: bold">.</span>getCount<span style="font-weight: bold">((</span>state<span style="font-weight: bold">, </span>action<span style="font-weight: bold">)) </span><span style="color: blue; font-weight: bold">for </span>action <span style="color: blue; font-weight: bold">in </span>actions<span style="font-weight: bold">])
        </span>bestActions <span style="font-weight: bold">= [</span>action <span style="color: blue; font-weight: bold">for </span>action <span style="color: blue; font-weight: bold">in </span>actions <span style="color: blue; font-weight: bold">if </span>qValues<span style="font-weight: bold">.</span>getCount<span style="font-weight: bold">((</span>state<span style="font-weight: bold">, </span>action<span style="font-weight: bold">)) == </span>bestQ<span style="font-weight: bold">]
    
        </span><span style="color: green; font-style: italic"># display cell
        </span>qStrings <span style="font-weight: bold">= </span>dict<span style="font-weight: bold">([(</span>action<span style="font-weight: bold">, </span><span style="color: red">"%.2f" </span><span style="font-weight: bold">% </span>qValues<span style="font-weight: bold">.</span>getCount<span style="font-weight: bold">((</span>state<span style="font-weight: bold">, </span>action<span style="font-weight: bold">))) </span><span style="color: blue; font-weight: bold">for </span>action <span style="color: blue; font-weight: bold">in </span>actions<span style="font-weight: bold">])
        </span>northString <span style="font-weight: bold">= (</span><span style="color: red">'north' </span><span style="color: blue; font-weight: bold">in </span>qStrings <span style="color: blue; font-weight: bold">and </span>qStrings<span style="font-weight: bold">[</span><span style="color: red">'north'</span><span style="font-weight: bold">]) </span><span style="color: blue; font-weight: bold">or </span><span style="color: red">' '
        </span>southString <span style="font-weight: bold">= (</span><span style="color: red">'south' </span><span style="color: blue; font-weight: bold">in </span>qStrings <span style="color: blue; font-weight: bold">and </span>qStrings<span style="font-weight: bold">[</span><span style="color: red">'south'</span><span style="font-weight: bold">]) </span><span style="color: blue; font-weight: bold">or </span><span style="color: red">' '
        </span>eastString <span style="font-weight: bold">= (</span><span style="color: red">'east' </span><span style="color: blue; font-weight: bold">in </span>qStrings <span style="color: blue; font-weight: bold">and </span>qStrings<span style="font-weight: bold">[</span><span style="color: red">'east'</span><span style="font-weight: bold">]) </span><span style="color: blue; font-weight: bold">or </span><span style="color: red">' '
        </span>westString <span style="font-weight: bold">= (</span><span style="color: red">'west' </span><span style="color: blue; font-weight: bold">in </span>qStrings <span style="color: blue; font-weight: bold">and </span>qStrings<span style="font-weight: bold">[</span><span style="color: red">'west'</span><span style="font-weight: bold">]) </span><span style="color: blue; font-weight: bold">or </span><span style="color: red">' '
        </span>exitString <span style="font-weight: bold">= (</span><span style="color: red">'exit' </span><span style="color: blue; font-weight: bold">in </span>qStrings <span style="color: blue; font-weight: bold">and </span>qStrings<span style="font-weight: bold">[</span><span style="color: red">'exit'</span><span style="font-weight: bold">]) </span><span style="color: blue; font-weight: bold">or </span><span style="color: red">' '

        </span>eastLen <span style="font-weight: bold">= </span>len<span style="font-weight: bold">(</span>eastString<span style="font-weight: bold">)
        </span>westLen <span style="font-weight: bold">= </span>len<span style="font-weight: bold">(</span>westString<span style="font-weight: bold">)
        </span><span style="color: blue; font-weight: bold">if </span>eastLen <span style="font-weight: bold">&lt; </span>westLen<span style="font-weight: bold">:
          </span>eastString <span style="font-weight: bold">= </span><span style="color: red">' '</span><span style="font-weight: bold">*(</span>westLen<span style="font-weight: bold">-</span>eastLen<span style="font-weight: bold">)+</span>eastString
        <span style="color: blue; font-weight: bold">if </span>westLen <span style="font-weight: bold">&lt; </span>eastLen<span style="font-weight: bold">:
          </span>westString <span style="font-weight: bold">= </span>westString<span style="font-weight: bold">+</span><span style="color: red">' '</span><span style="font-weight: bold">*(</span>eastLen<span style="font-weight: bold">-</span>westLen<span style="font-weight: bold">)
    
        </span><span style="color: blue; font-weight: bold">if </span><span style="color: red">'north' </span><span style="color: blue; font-weight: bold">in </span>bestActions<span style="font-weight: bold">:
          </span>northString <span style="font-weight: bold">= </span><span style="color: red">'/'</span><span style="font-weight: bold">+</span>northString<span style="font-weight: bold">+</span>'\\'
        <span style="color: blue; font-weight: bold">if </span><span style="color: red">'south' </span><span style="color: blue; font-weight: bold">in </span>bestActions<span style="font-weight: bold">:
          </span>southString <span style="font-weight: bold">= </span><span style="color: red">'\\'+southString+'</span><span style="font-weight: bold">/</span>'
        <span style="color: blue; font-weight: bold">if </span><span style="color: red">'east' </span><span style="color: blue; font-weight: bold">in </span>bestActions<span style="font-weight: bold">:
          </span>eastString <span style="font-weight: bold">= </span><span style="color: red">''</span><span style="font-weight: bold">+</span>eastString<span style="font-weight: bold">+</span><span style="color: red">'&gt;'
        </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
          </span>eastString <span style="font-weight: bold">= </span><span style="color: red">''</span><span style="font-weight: bold">+</span>eastString<span style="font-weight: bold">+</span><span style="color: red">' '
        </span><span style="color: blue; font-weight: bold">if </span><span style="color: red">'west' </span><span style="color: blue; font-weight: bold">in </span>bestActions<span style="font-weight: bold">:
          </span>westString <span style="font-weight: bold">= </span><span style="color: red">'&lt;'</span><span style="font-weight: bold">+</span>westString<span style="font-weight: bold">+</span><span style="color: red">''
        </span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
          </span>westString <span style="font-weight: bold">= </span><span style="color: red">' '</span><span style="font-weight: bold">+</span>westString<span style="font-weight: bold">+</span><span style="color: red">''
        </span><span style="color: blue; font-weight: bold">if </span><span style="color: red">'exit' </span><span style="color: blue; font-weight: bold">in </span>bestActions<span style="font-weight: bold">:
          </span>exitString <span style="font-weight: bold">= </span><span style="color: red">'[ '</span><span style="font-weight: bold">+</span>exitString<span style="font-weight: bold">+</span><span style="color: red">' ]'

    
        </span>ewString <span style="font-weight: bold">= </span>westString <span style="font-weight: bold">+ </span><span style="color: red">"     " </span><span style="font-weight: bold">+ </span>eastString
        <span style="color: blue; font-weight: bold">if </span>state <span style="font-weight: bold">== </span>currentState<span style="font-weight: bold">:
          </span>ewString <span style="font-weight: bold">= </span>westString <span style="font-weight: bold">+ </span><span style="color: red">"  *  " </span><span style="font-weight: bold">+ </span>eastString
        <span style="color: blue; font-weight: bold">if </span>state <span style="font-weight: bold">== </span>gridWorld<span style="font-weight: bold">.</span>getStartState<span style="font-weight: bold">():
          </span>ewString <span style="font-weight: bold">= </span>westString <span style="font-weight: bold">+ </span><span style="color: red">"  S  " </span><span style="font-weight: bold">+ </span>eastString
        <span style="color: blue; font-weight: bold">if </span>state <span style="font-weight: bold">== </span>currentState <span style="color: blue; font-weight: bold">and </span>state <span style="font-weight: bold">== </span>gridWorld<span style="font-weight: bold">.</span>getStartState<span style="font-weight: bold">():
          </span>ewString <span style="font-weight: bold">= </span>westString <span style="font-weight: bold">+ </span><span style="color: red">" S:* " </span><span style="font-weight: bold">+ </span>eastString
    
        text <span style="font-weight: bold">= [</span>northString<span style="font-weight: bold">, </span><span style="color: red">"\n"</span><span style="font-weight: bold">+</span>exitString<span style="font-weight: bold">, </span>ewString<span style="font-weight: bold">, </span><span style="color: red">' '</span><span style="font-weight: bold">*</span>maxLen<span style="font-weight: bold">+</span><span style="color: red">"\n"</span><span style="font-weight: bold">, </span>southString<span style="font-weight: bold">]
    
        </span><span style="color: blue; font-weight: bold">if </span>grid<span style="font-weight: bold">[</span>x<span style="font-weight: bold">][</span>y<span style="font-weight: bold">] == </span><span style="color: red">'#'</span><span style="font-weight: bold">:
          </span>text <span style="font-weight: bold">= [</span><span style="color: red">''</span><span style="font-weight: bold">, </span><span style="color: red">'\n#####\n#####\n#####'</span><span style="font-weight: bold">, </span><span style="color: red">''</span><span style="font-weight: bold">]
    
        </span>newCell <span style="font-weight: bold">= </span><span style="color: red">"\n"</span><span style="font-weight: bold">.</span>join<span style="font-weight: bold">(</span>text<span style="font-weight: bold">)
        </span>newRow<span style="font-weight: bold">.</span>append<span style="font-weight: bold">(</span>newCell<span style="font-weight: bold">)
      </span>newRows<span style="font-weight: bold">.</span>append<span style="font-weight: bold">(</span>newRow<span style="font-weight: bold">)
    </span>numCols <span style="font-weight: bold">= </span>grid<span style="font-weight: bold">.</span>width
    <span style="color: blue; font-weight: bold">for </span>rowNum<span style="font-weight: bold">, </span>row <span style="color: blue; font-weight: bold">in </span>enumerate<span style="font-weight: bold">(</span>newRows<span style="font-weight: bold">):
      </span>row<span style="font-weight: bold">.</span>insert<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">"\n\n\n"</span><span style="font-weight: bold">+</span>str<span style="font-weight: bold">(</span>rowNum<span style="font-weight: bold">))
    </span>newRows<span style="font-weight: bold">.</span>reverse<span style="font-weight: bold">()
    </span>colLabels <span style="font-weight: bold">= [</span>str<span style="font-weight: bold">(</span>colNum<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>colNum <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>numCols<span style="font-weight: bold">)]
    </span>colLabels<span style="font-weight: bold">.</span>insert<span style="font-weight: bold">(</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">' '</span><span style="font-weight: bold">)
    </span>finalRows <span style="font-weight: bold">= [</span>colLabels<span style="font-weight: bold">] + </span>newRows

    <span style="color: blue; font-weight: bold">print </span>indent<span style="font-weight: bold">(</span>finalRows<span style="font-weight: bold">,</span>separateRows<span style="font-weight: bold">=</span><span style="color: blue; font-weight: bold">True</span><span style="font-weight: bold">,</span>delim<span style="font-weight: bold">=</span><span style="color: red">'|'</span><span style="font-weight: bold">,</span>prefix<span style="font-weight: bold">=</span><span style="color: red">'|'</span><span style="font-weight: bold">,</span>postfix<span style="font-weight: bold">=</span><span style="color: red">'|'</span><span style="font-weight: bold">, </span>justify<span style="font-weight: bold">=</span><span style="color: red">'center'</span><span style="font-weight: bold">,</span>hasHeader<span style="font-weight: bold">=</span><span style="color: blue; font-weight: bold">True</span><span style="font-weight: bold">)

</span><span style="color: blue; font-weight: bold">def </span>border<span style="font-weight: bold">(</span>text<span style="font-weight: bold">):    
  </span>length <span style="font-weight: bold">= </span>len<span style="font-weight: bold">(</span>text<span style="font-weight: bold">)
  </span>pieces <span style="font-weight: bold">= [</span><span style="color: red">'-' </span><span style="font-weight: bold">* (</span>length<span style="font-weight: bold">+</span><span style="color: red">2</span><span style="font-weight: bold">), </span><span style="color: red">'|'</span><span style="font-weight: bold">+</span><span style="color: red">' ' </span><span style="font-weight: bold">* (</span>length<span style="font-weight: bold">+</span><span style="color: red">2</span><span style="font-weight: bold">)+</span><span style="color: red">'|'</span><span style="font-weight: bold">, </span><span style="color: red">' | '</span><span style="font-weight: bold">+</span>text<span style="font-weight: bold">+</span><span style="color: red">' | '</span><span style="font-weight: bold">, </span><span style="color: red">'|'</span><span style="font-weight: bold">+</span><span style="color: red">' ' </span><span style="font-weight: bold">* (</span>length<span style="font-weight: bold">+</span><span style="color: red">2</span><span style="font-weight: bold">)+</span><span style="color: red">'|'</span><span style="font-weight: bold">,</span><span style="color: red">'-' </span><span style="font-weight: bold">* (</span>length<span style="font-weight: bold">+</span><span style="color: red">2</span><span style="font-weight: bold">)]
  </span><span style="color: blue; font-weight: bold">return </span><span style="color: red">'\n'</span><span style="font-weight: bold">.</span>join<span style="font-weight: bold">(</span>pieces<span style="font-weight: bold">)
    
</span><span style="color: green; font-style: italic"># INDENTING CODE

# Indenting code based on a post from George Sakkis
# (http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/267662)

</span><span style="color: blue; font-weight: bold">import </span>cStringIO<span style="font-weight: bold">,</span>operator

<span style="color: blue; font-weight: bold">def </span>indent<span style="font-weight: bold">(</span>rows<span style="font-weight: bold">, </span>hasHeader<span style="font-weight: bold">=</span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">, </span>headerChar<span style="font-weight: bold">=</span><span style="color: red">'-'</span><span style="font-weight: bold">, </span>delim<span style="font-weight: bold">=</span><span style="color: red">' | '</span><span style="font-weight: bold">, </span>justify<span style="font-weight: bold">=</span><span style="color: red">'left'</span><span style="font-weight: bold">,
           </span>separateRows<span style="font-weight: bold">=</span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">, </span>prefix<span style="font-weight: bold">=</span><span style="color: red">''</span><span style="font-weight: bold">, </span>postfix<span style="font-weight: bold">=</span><span style="color: red">''</span><span style="font-weight: bold">, </span>wrapfunc<span style="font-weight: bold">=</span><span style="color: blue; font-weight: bold">lambda </span>x<span style="font-weight: bold">:</span>x<span style="font-weight: bold">):
    </span><span style="color: darkred">"""Indents a table by column.
       - rows: A sequence of sequences of items, one sequence per row.
       - hasHeader: True if the first row consists of the columns' names.
       - headerChar: Character to be used for the row separator line
         (if hasHeader==True or separateRows==True).
       - delim: The column delimiter.
       - justify: Determines how are data justified in their column. 
         Valid values are 'left','right' and 'center'.
       - separateRows: True if rows are to be separated by a line
         of 'headerChar's.
       - prefix: A string prepended to each printed row.
       - postfix: A string appended to each printed row.
       - wrapfunc: A function f(text) for wrapping text; each element in
         the table is first wrapped by this function."""
    </span><span style="color: green; font-style: italic"># closure for breaking logical rows to physical, using wrapfunc
    </span><span style="color: blue; font-weight: bold">def </span>rowWrapper<span style="font-weight: bold">(</span>row<span style="font-weight: bold">):
        </span>newRows <span style="font-weight: bold">= [</span>wrapfunc<span style="font-weight: bold">(</span>item<span style="font-weight: bold">).</span>split<span style="font-weight: bold">(</span><span style="color: red">'\n'</span><span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>item <span style="color: blue; font-weight: bold">in </span>row<span style="font-weight: bold">]
        </span><span style="color: blue; font-weight: bold">return </span><span style="font-weight: bold">[[</span>substr <span style="color: blue; font-weight: bold">or </span><span style="color: red">'' </span><span style="color: blue; font-weight: bold">for </span>substr <span style="color: blue; font-weight: bold">in </span>item<span style="font-weight: bold">] </span><span style="color: blue; font-weight: bold">for </span>item <span style="color: blue; font-weight: bold">in </span>map<span style="font-weight: bold">(</span><span style="color: blue">None</span><span style="font-weight: bold">,*</span>newRows<span style="font-weight: bold">)]
    </span><span style="color: green; font-style: italic"># break each logical row into one or more physical ones
    </span>logicalRows <span style="font-weight: bold">= [</span>rowWrapper<span style="font-weight: bold">(</span>row<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>row <span style="color: blue; font-weight: bold">in </span>rows<span style="font-weight: bold">]
    </span><span style="color: green; font-style: italic"># columns of physical rows
    </span>columns <span style="font-weight: bold">= </span>map<span style="font-weight: bold">(</span><span style="color: blue">None</span><span style="font-weight: bold">,*</span>reduce<span style="font-weight: bold">(</span>operator<span style="font-weight: bold">.</span>add<span style="font-weight: bold">,</span>logicalRows<span style="font-weight: bold">))
    </span><span style="color: green; font-style: italic"># get the maximum of each column by the string length of its items
    </span>maxWidths <span style="font-weight: bold">= [</span>max<span style="font-weight: bold">([</span>len<span style="font-weight: bold">(</span>str<span style="font-weight: bold">(</span>item<span style="font-weight: bold">)) </span><span style="color: blue; font-weight: bold">for </span>item <span style="color: blue; font-weight: bold">in </span>column<span style="font-weight: bold">]) </span><span style="color: blue; font-weight: bold">for </span>column <span style="color: blue; font-weight: bold">in </span>columns<span style="font-weight: bold">]
    </span>rowSeparator <span style="font-weight: bold">= </span>headerChar <span style="font-weight: bold">* (</span>len<span style="font-weight: bold">(</span>prefix<span style="font-weight: bold">) + </span>len<span style="font-weight: bold">(</span>postfix<span style="font-weight: bold">) + </span>sum<span style="font-weight: bold">(</span>maxWidths<span style="font-weight: bold">) + </span>\
                                 len<span style="font-weight: bold">(</span>delim<span style="font-weight: bold">)*(</span>len<span style="font-weight: bold">(</span>maxWidths<span style="font-weight: bold">)-</span><span style="color: red">1</span><span style="font-weight: bold">))
    </span><span style="color: green; font-style: italic"># select the appropriate justify method
    </span>justify <span style="font-weight: bold">= {</span><span style="color: red">'center'</span><span style="font-weight: bold">:</span>str<span style="font-weight: bold">.</span>center<span style="font-weight: bold">, </span><span style="color: red">'right'</span><span style="font-weight: bold">:</span>str<span style="font-weight: bold">.</span>rjust<span style="font-weight: bold">, </span><span style="color: red">'left'</span><span style="font-weight: bold">:</span>str<span style="font-weight: bold">.</span>ljust<span style="font-weight: bold">}[</span>justify<span style="font-weight: bold">.</span>lower<span style="font-weight: bold">()]
    </span>output<span style="font-weight: bold">=</span>cStringIO<span style="font-weight: bold">.</span>StringIO<span style="font-weight: bold">()
    </span><span style="color: blue; font-weight: bold">if </span>separateRows<span style="font-weight: bold">: </span><span style="color: blue; font-weight: bold">print </span><span style="font-weight: bold">&gt;&gt; </span>output<span style="font-weight: bold">, </span>rowSeparator
    <span style="color: blue; font-weight: bold">for </span>physicalRows <span style="color: blue; font-weight: bold">in </span>logicalRows<span style="font-weight: bold">:
        </span><span style="color: blue; font-weight: bold">for </span>row <span style="color: blue; font-weight: bold">in </span>physicalRows<span style="font-weight: bold">:
            </span><span style="color: blue; font-weight: bold">print </span><span style="font-weight: bold">&gt;&gt; </span>output<span style="font-weight: bold">, </span>\
                prefix \
                <span style="font-weight: bold">+ </span>delim<span style="font-weight: bold">.</span>join<span style="font-weight: bold">([</span>justify<span style="font-weight: bold">(</span>str<span style="font-weight: bold">(</span>item<span style="font-weight: bold">),</span>width<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span><span style="font-weight: bold">(</span>item<span style="font-weight: bold">,</span>width<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">in </span>zip<span style="font-weight: bold">(</span>row<span style="font-weight: bold">,</span>maxWidths<span style="font-weight: bold">)]) </span>\
                <span style="font-weight: bold">+ </span>postfix
        <span style="color: blue; font-weight: bold">if </span>separateRows <span style="color: blue; font-weight: bold">or </span>hasHeader<span style="font-weight: bold">: </span><span style="color: blue; font-weight: bold">print </span><span style="font-weight: bold">&gt;&gt; </span>output<span style="font-weight: bold">, </span>rowSeparator; hasHeader<span style="font-weight: bold">=</span><span style="color: blue; font-weight: bold">False
    return </span>output<span style="font-weight: bold">.</span>getvalue<span style="font-weight: bold">()
    
</span><span style="color: blue; font-weight: bold">import </span>math
<span style="color: blue; font-weight: bold">def </span>wrap_always<span style="font-weight: bold">(</span>text<span style="font-weight: bold">, </span>width<span style="font-weight: bold">):
    </span><span style="color: darkred">"""A simple word-wrap function that wraps text on exactly width characters.
       It doesn't split the text in words."""
    </span><span style="color: blue; font-weight: bold">return </span><span style="color: red">'\n'</span><span style="font-weight: bold">.</span>join<span style="font-weight: bold">([ </span>text<span style="font-weight: bold">[</span>width<span style="font-weight: bold">*</span>i<span style="font-weight: bold">:</span>width<span style="font-weight: bold">*(</span>i<span style="font-weight: bold">+</span><span style="color: red">1</span><span style="font-weight: bold">)] </span>\
                       <span style="color: blue; font-weight: bold">for </span>i <span style="color: blue; font-weight: bold">in </span>xrange<span style="font-weight: bold">(</span>int<span style="font-weight: bold">(</span>math<span style="font-weight: bold">.</span>ceil<span style="font-weight: bold">(</span><span style="color: red">1</span><span style="font-weight: bold">.*</span>len<span style="font-weight: bold">(</span>text<span style="font-weight: bold">)/</span>width<span style="font-weight: bold">))) ])
    
    
</span><span style="color: green; font-style: italic"># TEST OF DISPLAY CODE
                                
</span><span style="color: blue; font-weight: bold">if </span>__name__ <span style="font-weight: bold">== </span><span style="color: red">'__main__'</span><span style="font-weight: bold">:
  </span><span style="color: blue; font-weight: bold">import </span>gridworld<span style="font-weight: bold">, </span>util

  grid <span style="font-weight: bold">= </span>gridworld<span style="font-weight: bold">.</span>getCliffGrid3<span style="font-weight: bold">()
  </span><span style="color: blue; font-weight: bold">print </span>grid<span style="font-weight: bold">.</span>getStates<span style="font-weight: bold">()
  
  </span>policy <span style="font-weight: bold">= </span>dict<span style="font-weight: bold">([(</span>state<span style="font-weight: bold">,</span><span style="color: red">'east'</span><span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>state <span style="color: blue; font-weight: bold">in </span>grid<span style="font-weight: bold">.</span>getStates<span style="font-weight: bold">()])
  </span>values <span style="font-weight: bold">= </span>util<span style="font-weight: bold">.</span>Counter<span style="font-weight: bold">(</span>dict<span style="font-weight: bold">([(</span>state<span style="font-weight: bold">,</span><span style="color: red">1000.23</span><span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>state <span style="color: blue; font-weight: bold">in </span>grid<span style="font-weight: bold">.</span>getStates<span style="font-weight: bold">()]))
  </span>prettyPrintValues<span style="font-weight: bold">(</span>grid<span style="font-weight: bold">, </span>values<span style="font-weight: bold">, </span>policy<span style="font-weight: bold">, </span>currentState <span style="font-weight: bold">= (</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">))

  </span>stateCrossActions <span style="font-weight: bold">= [[(</span>state<span style="font-weight: bold">, </span>action<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>action <span style="color: blue; font-weight: bold">in </span>grid<span style="font-weight: bold">.</span>getPossibleActions<span style="font-weight: bold">(</span>state<span style="font-weight: bold">)] </span><span style="color: blue; font-weight: bold">for </span>state <span style="color: blue; font-weight: bold">in </span>grid<span style="font-weight: bold">.</span>getStates<span style="font-weight: bold">()]
  </span>qStates <span style="font-weight: bold">= </span>reduce<span style="font-weight: bold">(</span><span style="color: blue; font-weight: bold">lambda </span>x<span style="font-weight: bold">,</span>y<span style="font-weight: bold">: </span>x<span style="font-weight: bold">+</span>y<span style="font-weight: bold">, </span>stateCrossActions<span style="font-weight: bold">, [])
  </span>qValues <span style="font-weight: bold">= </span>util<span style="font-weight: bold">.</span>Counter<span style="font-weight: bold">(</span>dict<span style="font-weight: bold">([((</span>state<span style="font-weight: bold">, </span>action<span style="font-weight: bold">), </span><span style="color: red">10.5</span><span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>state<span style="font-weight: bold">, </span>action <span style="color: blue; font-weight: bold">in </span>qStates<span style="font-weight: bold">]))
  </span>qValues <span style="font-weight: bold">= </span>util<span style="font-weight: bold">.</span>Counter<span style="font-weight: bold">(</span>dict<span style="font-weight: bold">([((</span>state<span style="font-weight: bold">, </span>action<span style="font-weight: bold">), </span><span style="color: red">10.5</span><span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>state<span style="font-weight: bold">, </span>action <span style="color: blue; font-weight: bold">in </span>reduce<span style="font-weight: bold">(</span><span style="color: blue; font-weight: bold">lambda </span>x<span style="font-weight: bold">,</span>y<span style="font-weight: bold">: </span>x<span style="font-weight: bold">+</span>y<span style="font-weight: bold">, </span>stateCrossActions<span style="font-weight: bold">, [])]))
  </span>prettyPrintQValues<span style="font-weight: bold">(</span>grid<span style="font-weight: bold">, </span>qValues<span style="font-weight: bold">, </span>currentState <span style="font-weight: bold">= (</span><span style="color: red">0</span><span style="font-weight: bold">,</span><span style="color: red">0</span><span style="font-weight: bold">))
</span>
  </pre>
  </body>
  </html>
  